const { Model, DataTypes } = require('sequelize');
const bcrypt = require('bcryptjs');

module.exports = (sequelize) => {
    class Admin extends Model {
        static associate(models) {
            // 定义关联关系
            Admin.belongsTo(models.Role, {
                foreignKey: 'role_id',
                as: 'role'
            });
        }

        // 验证密码
        async validatePassword(password) {
            return bcrypt.compare(password, this.password);
        }
    }

    Admin.init(
        {
            id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
                comment: '管理员ID'
            },
            username: {
                type: DataTypes.STRING(50),
                allowNull: false,
                unique: true,
                comment: '用户名'
            },
            password: {
                type: DataTypes.STRING(255),
                allowNull: false,
                comment: '密码'
            },
            salt: {
                type: DataTypes.STRING(255),
                allowNull: false,
                defaultValue: '',
                comment: '密码加密盐'
            },
            real_name: {
                type: DataTypes.STRING(50),
                comment: '真实姓名'
            },
            phone: {
                type: DataTypes.STRING(20),
                comment: '手机号'
            },
            email: {
                type: DataTypes.STRING(100),
                comment: '邮箱'
            },
            avatar_url: {
                type: DataTypes.STRING(255),
                comment: '头像URL'
            },
            role_id: {
                type: DataTypes.INTEGER,
                allowNull: false,
                comment: '角色ID'
            },
            last_login_time: {
                type: DataTypes.DATE,
                allowNull: true,
                comment: '最后登录时间'
            },
            status: {
                type: DataTypes.TINYINT,
                defaultValue: 1,
                comment: '状态：1-启用，0-禁用'
            },
            deleted_at: DataTypes.DATE
        },
        {
            sequelize,
            modelName: 'Admin',
            tableName: 'admins',
            timestamps: true,
            paranoid: true,
            underscored: true,
            hooks: {
                beforeCreate: async (admin) => {
                    if (admin.password) {
                        const salt = await bcrypt.genSalt(10);
                        admin.salt = salt;
                        admin.password = await bcrypt.hash(admin.password, salt);
                    }
                },
                beforeUpdate: async (admin) => {
                    if (admin.changed('password')) {
                        const salt = await bcrypt.genSalt(10);
                        admin.salt = salt;
                        admin.password = await bcrypt.hash(admin.password, salt);
                    }
                }
            }
        }
    );

    return Admin;
};